package com.jia.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.pojo.*;
import com.jia.service.AuthorityService;
import com.jia.service.RoleAuthorityService;
import com.jia.service.RoleService;
import com.jia.vo.RoleList;
import com.jia.vo.RoleSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 贾宇光
 * @version 1.0
 * @date 2020/9/17 9:42
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    AuthorityService authorityService;
    @Autowired
    RoleAuthorityService roleAuthorityService;


    @GetMapping("/select")
    public String roleSelect(Model model, HttpSession session) {
        MyUser myUserInfo = (MyUser) session.getAttribute("myUserInfo");
        Role role = roleService.queryRoleByUid(myUserInfo.getUid());
        List<RoleList> roleLists = roleAuthorityService.queryRoleAuthority(role.getRid());
        Map<Integer, RoleSelect> roleSelectMap = new HashMap<>();
        for (RoleList roleList : roleLists) {
            if (roleSelectMap.containsKey(roleList.getRid())) {
                RoleSelect roleSelect = roleSelectMap.get(roleList.getRid());
                roleSelect.getAid().add(roleList.getAid());
                roleSelect.getAname().add(roleList.getAname());
                roleSelect.getAdescription().add(roleList.getAdescription());
            } else {
                RoleSelect roleSelect = new RoleSelect();
                roleSelect.setRid(roleList.getRid());
                roleSelect.setRname(roleList.getRname());
                roleSelect.setRdescription(roleList.getRdescription());
                ArrayList<Integer> aids = new ArrayList<>();
                aids.add(roleList.getAid());
                roleSelect.setAid(aids);
                ArrayList<String> anames = new ArrayList<>();
                anames.add(roleList.getAname());
                roleSelect.setAname(anames);
                ArrayList<String> adescriptions = new ArrayList<>();
                adescriptions.add(roleList.getAdescription());
                roleSelect.setAdescription(adescriptions);
                roleSelectMap.put(roleList.getRid(), roleSelect);
            }
        }
        System.out.println(roleSelectMap);
        model.addAttribute("roleSelectMap", roleSelectMap);
        return "pages/role/selectRole";
    }

    @GetMapping("/update/{rid}/{aid}/{rname}")
    public String roleUpdate(@PathVariable("rid") int rid,
                             @PathVariable("aid") String aid,
                             @PathVariable("rname") String rname,
                             Model model) {
        List<Authority> authorityList = authorityService.list(null);
        model.addAttribute("authorityList", authorityList);
        model.addAttribute("rid", rid);
        model.addAttribute("aid", aid.substring(1, aid.length() - 1));
        model.addAttribute("rname", rname);
        return "pages/role/updateRole";
    }

    @PostMapping("/updateData")
    public String roleUpdateData(int rid, String[] authoritys) {
        roleAuthorityService.remove(new QueryWrapper<RoleAuthority>().eq("rid", rid));
        for (String aid : authoritys) {
            RoleAuthority roleAuthority = new RoleAuthority();
            roleAuthority.setRid(rid);
            roleAuthority.setAid(Integer.valueOf(aid));
            roleAuthorityService.save(roleAuthority);
        }
        return "index";
    }
}
